package mac10.util;

import java.util.ArrayList;

import mac10.models.Cell;
import mac10.models.Cluster;
import mac10.models.WorldModel;
import mac10.monitor.InfoMonitor;

public class SimpleCluster
{

    private WorldModel world;
    int e, minPts;

    ArrayList<Cluster> all;
    
    final int MAX_SIZE = Integer.MAX_VALUE;
    
    public SimpleCluster (WorldModel worldModel)
    {
        this.world = worldModel;
        
        
       
    }
    
    public ArrayList<Cluster> getClusters(ArrayList<Cell> d)
    {      
        ArrayList<Cluster> clusters = new ArrayList<Cluster>();
        
        while (!d.isEmpty())
        {
            Cell cell = d.remove(0);
            Cluster cluster = newCluster(cell);
            
            d.removeAll(cluster.getList());
            clusters.add(cluster);
        }
        
        all = clusters;
        return clusters;
    }
    
    
    public Cluster newCluster(Cell p)
    {
        ArrayList<Cell> cluster = new ArrayList<Cell>();
        ArrayList<Cell> tmp = new ArrayList<Cell>();
        
        
        ArrayList<Cell> initNeighbours = world.getCowNeighbours(p, 3);        
        cluster.addAll(initNeighbours);
        tmp.addAll(initNeighbours);
        tmp.remove(p);
        
        
        while(!tmp.isEmpty())
        {
            Cell cell = tmp.remove(tmp.size()-1);
            ArrayList<Cell> neighbours = world.getCowNeighbours(cell, 3);
            
            
            // nachbarn von cows mit weniger als 3 nachbarn werden nicht hinzugefügt
            // das sind 'rand' cows
            // hier is fein tuning angesagt
            if(world.getCowNeighbours(cell, 1).size() >= 2 && world.getCowNeighbours(cell, 2).size() >= 4)
            {
                for (Cell cell2 : neighbours)
                {
                    if(!cluster.contains(cell2))
                    {
                        if(cluster.size() >= MAX_SIZE)
                            break;
                        
                        cluster.add(cell2);
                        tmp.add(cell2);
                    }
                }
            }

        }
        
        return new Cluster(cluster);
    }
    
    

    
    
    public void clusterDebug()
    {
        InfoMonitor.staticMonitor.setRightText("");
        
        for (Cluster list : all)
        {
            String s = "";
            for (Cell cell : list.getList())
            {
                s+=cell.toStringShort();
            }
            InfoMonitor.staticMonitor.addLineToRightText(s+"\n");
        }
        
    }
    
}
